package com.zzh.netty.nettyServer;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.util.CharsetUtil;

/**
 * @PackageName:com.zzh.netty.nettyServer
 * @ClassName:NettyServerHandler
 * @Description:
 * @author:zhangzhanheng
 * @date: 2021/1/30 14:23
 */
public class NettyServerHandler extends ChannelInboundHandlerAdapter {

    //读取数据
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        System.out.println("服务器读取线程"+Thread.currentThread().getName());
        ByteBuf byteBuf = (ByteBuf)msg;
        System.out.println("读取客户端数据："+byteBuf.toString(CharsetUtil.UTF_8));
//        super.channelRead(ctx, msg);
    }
    //读取完数据进行处理
    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
        ByteBuf buf = Unpooled.copiedBuffer("HelloClient".getBytes());
        ctx.writeAndFlush(buf);

//        super.channelReadComplete(ctx);
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
//        super.exceptionCaught(ctx, cause);
    }
}
